package khs.java_http_post.main.java;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws IOException
    {
        System.out.print("input url: ");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String input = bufferedReader.readLine();
        
        System.out.print("url parameters: ");
        String params = bufferedReader.readLine();
        
        URL url = new URL(input);
        HttpURLConnection httpUrlConnection = (HttpURLConnection)url.openConnection();
        
        httpUrlConnection.setRequestMethod("POST");
        httpUrlConnection.setDoOutput(true);
        DataOutputStream dataOutputStream = new DataOutputStream(httpUrlConnection.getOutputStream());
        dataOutputStream.writeBytes(params);
        dataOutputStream.flush();
        dataOutputStream.close();
        
        httpUrlConnection.connect();
        
        Map<String, List<String>>headerFields = httpUrlConnection.getHeaderFields();
        final int responseCode = httpUrlConnection.getResponseCode();
        final String responseMessage = httpUrlConnection.getResponseMessage();
        final int contentLength = httpUrlConnection.getContentLength();
        final String contentType = httpUrlConnection.getContentType();
        final URL responseURL = httpUrlConnection.getURL(); 
        
        System.out.println("header -> ");
        for(Map.Entry<String, List<String>>keyValue : headerFields.entrySet()){
            List<String> listValue = keyValue.getValue();
            
            System.out.println(keyValue.getKey() + ": ");
            System.out.println(listValue);
        }
        
        System.out.println("respnoseCode ->");
        System.out.println(responseCode);
        
        System.out.println("responseMessage ->");
        System.out.println(responseMessage);
        
        System.out.println("contentLength ->");
        System.out.println(contentLength);
        
        System.out.println("contentType ->");
        System.out.println(contentType);
        
        System.out.println("responseURL ->");
        System.out.println(responseURL.toString());
        
        
        final String response1 = getStreamContent(httpUrlConnection);
        
        
        System.out.println("");
        System.out.println("body ->");
        System.out.println(response1);
        
        httpUrlConnection.disconnect();
    }
    
    private static String getStreamContent(HttpURLConnection connection) throws IOException{
        InputStream inputStream = connection.getInputStream();
        
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        StringBuffer response = new StringBuffer();
        while((line = bufferedReader.readLine()) != null){
            response.append(line);
            response.append('\n');
        }
        bufferedReader.close();
       return response.toString();
    }
}
